﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace System.Web
{
    public class ZData
    {
        public static Dictionary<string, DataTable> _DataTable;
        public static Dictionary<string, DataTable> DataTable
        {
            get
            {
                if (_DataTable == null)
                    _DataTable = new Dictionary<string, DataTable>();

                return _DataTable;

            }
        }

        public static DataTable GetTable(string tableName)
        {
            if (!DataTable.ContainsKey(tableName))
            {
                BaseDal dal = new BaseDal();
                DataTable.Add(tableName, dal.GetDataTable("SELECT * FROM " + tableName + "(NOLOCK)"));
            }

            return _DataTable[tableName];
        }
        public static void RefreshDataTable(string tableName)
        {
            if (!DataTable.ContainsKey(tableName))
            {
                BaseDal dal = new BaseDal();
                DataTable.Add(tableName, dal.GetDataTable("SELECT * FROM " + tableName + "(NOLOCK)"));
            }
            else
            {
                BaseDal dal = new BaseDal();
                DataTable[tableName] = dal.GetDataTable("SELECT * FROM " + tableName + "(NOLOCK)");
            }
        }
        public static DataRow GetDataRow(string tableName, string filter)
        {
            DataRow row = null;
            DataRow[] rows = ZData.GetTable(tableName).Select(filter);
            if (rows.Length > 0)
                row = rows[0];
            return row;
        }
    }
}
